﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GioHangViet.Areas.Admin.Models;
using GioHangViet.Models;

namespace GioHangViet.Areas.Admin.Controllers
{ 
    public class PublisherController : Controller
    {
        private readonly GoLungTungModel _db = new GoLungTungModel();
        private readonly ProcessAlias _alias = new ProcessAlias();

        //
        // GET: /Admin/Publisher/

        public ViewResult Index(int id)
        {
            if (id < 1)
            {
                id = 1;
            }
            ViewBag.CurrentPage = id;
            ViewBag.countRecord = _db.Publishers.Count();
            ViewBag.pageSize = Convert.ToDouble(Resources.Constant.PageSize);
            var model =
                _db.Publishers.ToList().Skip((id - 1) * Convert.ToInt32(Resources.Constant.PageSize)).Take(
                    Convert.ToInt32(Resources.Constant.PageSize));
            return View(model);
        }
        
        //
        // GET: /Admin/Publisher/Create

        public ActionResult Create()
        {
            var publisher = new Publisher{ Alias = "new"};
            return View(publisher);
        } 

        //
        // POST: /Admin/Publisher/Create

        [HttpPost]
        public ActionResult Create(Publisher publisher)
        {
            if (ModelState.IsValid)
            {
                publisher.Alias = _alias.CreateAlias(publisher.Name, "Publishers", "Alias");
                _db.Publishers.Add(publisher);
                _db.SaveChanges();
                return RedirectToAction("Index", new{ id = 1 });  
            }

            return View(publisher);
        }
        
        //
        // GET: /Admin/Publisher/Edit/5
 
        public ActionResult Edit(string id)
        {
            Publisher publisher = _db.Publishers.Find(id);
            return View(publisher);
        }

        //
        // POST: /Admin/Publisher/Edit/5

        [HttpPost]
        public ActionResult Edit(Publisher publisher)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(publisher).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index", new{ id = 1 });
            }
            return View(publisher);
        }

        //
        // GET: /Admin/Publisher/Delete/5
 
        public ActionResult Delete(string id)
        {
            Publisher publisher = _db.Publishers.Find(id);
            if (publisher != null)
            {
                _db.Publishers.Remove(publisher);
                _db.SaveChanges();
                return RedirectToAction("Index", new {id = 1});
            }
            return View("Error");
        }


        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}